package com.gange.interceptors;

import com.gange.annotations.RequiredToken;
import com.gange.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 定义和注册拦截器，
 * 在请求handle方法处理前，将一些公共的处理放在这里
 *
 */
@Component
@Slf4j
public class TokenInterceptor implements HandlerInterceptor {

    private RedisTemplate redisTemplate;

    @Autowired
    public TokenInterceptor(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //token: xxx
        //tokenKey: user
        if(handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            //1.获取当前请求方法上是否存在RequiredToken注解
            boolean requiredToken = handlerMethod.getMethod().isAnnotationPresent(RequiredToken.class);
            //2.存在该注解
            if (requiredToken) {
                //1.获取token信息
                String token = request.getParameter("token");
                log.info("当前传递的token为: {}", token);
                //2.拼接前缀
                String tokenKey = "session_" + token;
                //3.根据tokenKey获取用户信息
                User o = (User) redisTemplate.opsForValue().get(tokenKey);
                if (o == null) throw new RuntimeException("提示: 令牌无效,无效token!");
                //4.存储到当前请求的上下文中
                request.setAttribute("token", token);
                request.setAttribute("user", o);
            }
        }
        return true;
    }
}
